//
//  CJSTravelheadView.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/25.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSTravelheadView.h"
#import "UIImageView+WebCache.h"
#import "CJSDays.h"
#import "CJSPoi_infos.h"
#import "CJSCitiesViewController.h"

@interface CJSTravelheadView ()

/**
 *  用户头像
 */
@property (nonatomic, retain) UIImageView *userHeader;
/**
 *  用户名
 */
@property (nonatomic, retain) UILabel *userName;
/**
 *  题目
 */
@property (nonatomic, retain) UILabel *titleName;
/**
 *  时间
 */
@property (nonatomic, retain) UILabel *timeLabel;
/**
 *  天数
 */
@property (nonatomic, retain) UILabel *dayCount;
/**
 *  里程
 */
@property (nonatomic, retain) UILabel *distance;
/**
 *  喜欢
 */
@property (nonatomic, retain) UILabel *likeCount;

@property (nonatomic, retain) UIImageView *lineView;

@property (nonatomic, retain) UILabel *distanceLabel;

@property (nonatomic, retain) UILabel *likeLabel;

@property (nonatomic, retain) UIView *backgView;
/**
 *  景点
 */
@property (nonatomic, retain) UILabel *attraction;
/**
 *  餐饮
 */
@property (nonatomic, retain) UILabel *cater;

@property (nonatomic, retain) UILabel *place;

@property (nonatomic, retain) UIView *downView;

@property (nonatomic, retain) UIImageView *attractionImage;

@property (nonatomic, retain) UIImageView *caterImage;

@property (nonatomic, retain) UIImageView *placeImage;

@end

@implementation CJSTravelheadView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    //头
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, SCREEN_HEIGHT / 3)];
    self.headImage.contentMode = UIViewContentModeScaleAspectFill;
    self.headImage.layer.masksToBounds = YES;
    [self addSubview:self.headImage];
    
    //用户头像
    self.userHeader = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    self.userHeader.center = CGPointMake(SCREEN_WIDTH / 2, self.headImage.frame.size.height);
    self.userHeader.layer.cornerRadius = self.userHeader.frame.size.height / 2;
    self.userHeader.layer.masksToBounds = YES;
    [self addSubview:self.userHeader];
    //用户名
    self.userName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    self.userName.center = CGPointMake(SCREEN_WIDTH / 2, self.userHeader.center.y + 60);
    self.userName.textAlignment = NSTextAlignmentCenter;
    self.userName.textColor = [UIColor colorWithRed:0.72 green:0.71 blue:0.7 alpha:1];
    self.userName.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.userName];
    
    //title
    self.titleName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    self.titleName.center = CGPointMake(SCREEN_WIDTH / 2, self.userName.center.y + 30);
    self.titleName.textAlignment = NSTextAlignmentCenter;
    self.titleName.font = [UIFont systemFontOfSize:24 weight:0.2];
    self.titleName.textColor = [UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1];
    if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        self.titleName.shadowColor = [UIColor colorWithRed:0.79 green:0.78 blue:0.75 alpha:1];
        self.titleName.shadowOffset = CGSizeMake(0, 2);
    }
    [self addSubview:self.titleName];
    
    self.downView = [[UIView alloc] initWithFrame:CGRectMake(15, self.titleName.center.y + 40, SCREEN_WIDTH - 30, 300)];
    [self addSubview:self.downView];
    
    self.lineView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 60, 80)];
    [self.downView addSubview:self.lineView];
    
    //timeLabel
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.lineView.frame.origin.y + 20, (SCREEN_WIDTH - 60) / 3, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.font = [UIFont systemFontOfSize:14 weight:0.3];
    self.timeLabel.textColor = [UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1];
    [self.downView addSubview:self.timeLabel];
    
    
    //dayCount
    self.dayCount = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height)];
    self.dayCount.center = CGPointMake(self.timeLabel.center.x, self.timeLabel.center.y + 20);
    self.dayCount.textAlignment = NSTextAlignmentCenter;
    self.dayCount.font = [UIFont systemFontOfSize:15 weight:0.1];
    self.dayCount.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.62 alpha:1];
    [self.downView addSubview:self.dayCount];
    //distanceLabel
    self.distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 60) / 3 + 15, self.timeLabel.frame.origin.y, (SCREEN_WIDTH - 60) / 3, 20)];
    self.distanceLabel.textAlignment = NSTextAlignmentCenter;
    self.distanceLabel.font = [UIFont systemFontOfSize:14 weight:0.3];
    self.distanceLabel.textColor = [UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1];
    [self.downView addSubview:self.distanceLabel];
    //里程
    self.distance = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.distanceLabel.frame.size.width, self.distanceLabel.frame.size.height)];
    self.distance.center = CGPointMake(self.distanceLabel.center.x, self.distanceLabel.center.y + 20);
    self.distance.textAlignment = NSTextAlignmentCenter;
    self.distance.font = [UIFont systemFontOfSize:15 weight:0.1];
    self.distance.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.62 alpha:1];
    [self.downView addSubview:self.distance];
    
    //timeLabel
    self.likeLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 60) * 2 / 3 + 15, self.lineView.frame.origin.y + 20, (SCREEN_WIDTH - 60) / 3, 20)];
    self.likeLabel.textAlignment = NSTextAlignmentCenter;
    self.likeLabel.font = [UIFont systemFontOfSize:14 weight:0.3];
    self.likeLabel.textColor = [UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1];
    [self.downView addSubview:self.likeLabel];
    //喜欢
    self.likeCount = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.likeLabel.frame.size.width, self.likeLabel.frame.size.height)];
    self.likeCount.center = CGPointMake(self.likeLabel.center.x, self.likeLabel.center.y + 20);
    self.likeCount.textAlignment = NSTextAlignmentCenter;
    self.likeCount.font = [UIFont systemFontOfSize:15 weight:0.1];
    self.likeCount.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.62 alpha:1];
    [self.downView addSubview:self.likeCount];
    
    self.backgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.lineView.frame.size.height + 40, SCREEN_WIDTH - 30, 150)];
    self.backgView.backgroundColor = [UIColor whiteColor];
    [self.downView addSubview:self.backgView];
    self.backgView.hidden = YES;
    
    //    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //    [self.backgView addGestureRecognizer:tap];
    //    [tap release];
    
    UIView *upLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.backgView.frame.size.width, 3)];
    upLineView.backgroundColor = [UIColor colorWithRed:0.29 green:0.29 blue:0.3 alpha:1];
    [self.backgView addSubview:upLineView];
    //景点
    self.attractionImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 35, 35)];
    self.attractionImage.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
    self.attractionImage.userInteractionEnabled = YES;
    self.attractionImage.layer.cornerRadius = 3;
    self.attractionImage.layer.masksToBounds = YES;
    [self.backgView addSubview:self.attractionImage];
    
    self.attraction = [[UILabel alloc] initWithFrame:CGRectMake(60, 15, self.backgView.frame.size.width / 2 - 60, 35)];
    self.attraction.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
    self.attraction.userInteractionEnabled = YES;
    [self.backgView addSubview:self.attraction];
    //餐饮
    self.caterImage = [[UIImageView alloc] initWithFrame:CGRectMake(15 + self.backgView.center.x, 15, 35, 35)];
    self.caterImage.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
    self.caterImage.userInteractionEnabled = YES;
    self.caterImage.layer.cornerRadius = 3;
    self.caterImage.layer.masksToBounds = YES;
    [self.backgView addSubview:self.caterImage];
    
    self.cater = [[UILabel alloc] initWithFrame:CGRectMake(self.backgView.frame.size.width / 2 + 60, 15, self.backgView.frame.size.width / 2 - 60, 35)];
    self.cater.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
    self.cater.userInteractionEnabled = YES;
    [self.backgView addSubview:self.cater];
    //地点
    self.placeImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 65, 35, 35)];
    self.placeImage.layer.cornerRadius = self.placeImage.frame.size.width / 2;
    self.placeImage.userInteractionEnabled = YES;
    self.placeImage.layer.masksToBounds = YES;
    [self.backgView addSubview:self.placeImage];
    
    self.place = [[UILabel alloc] initWithFrame:CGRectMake(60, 65, self.backgView.frame.size.width - 100, 35)];
    self.place.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
    self.place.userInteractionEnabled = YES;
    [self.backgView addSubview:self.place];
    //查看路线
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, self.backgView.frame.size.height - 33, self.backgView.frame.size.width, 30)];
    line.textAlignment = NSTextAlignmentCenter;
    line.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    line.font = [UIFont systemFontOfSize:17 weight:0.2];
    line.textColor = [UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1];
    line.userInteractionEnabled = YES;
    [self.backgView addSubview:line];
    
    UIView *downLineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.backgView.frame.size.height - 3, self.backgView.frame.size.width, 3)];
    downLineView.backgroundColor = [UIColor colorWithRed:0.77 green:0.77 blue:0.78 alpha:1];
    [self.backgView addSubview:downLineView];
    
}

- (void)setTravel:(CJSTravel *)travel {
    
    _travel = travel;
 
    self.backgView.hidden = NO;
    //头视图
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:self.travel.cover_image] placeholderImage:[UIImage imageNamed:@"trip_edit_cover_default"]];
    //用户头像
    [self.userHeader sd_setImageWithURL:[NSURL URLWithString:self.travel.users.avatar_m] placeholderImage:[UIImage imageNamed:@"avatar_placeholder_26"]];
    self.userHeader.layer.borderWidth = 3;
    self.userHeader.layer.borderColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.94 alpha:1].CGColor;
    //用户名
    self.userName.text = [NSString stringWithFormat:@"by %@", self.travel.users.name];
    
    self.lineView.image = [UIImage imageNamed:@"user_info_fav_bg"];
    //题目
    self.titleName.text = self.travel.name;
    //日期
    CJSDays *days = [self.travel.dayArray firstObject];
    self.timeLabel.text = [days date];
    self.likeLabel.text = @"喜欢";
    self.distanceLabel.text = @"里程";
    //时长
    self.dayCount.text = [NSString stringWithFormat:@"%@天", self.travel.day_count];
    //里程
    self.distance.text = [NSString stringWithFormat:@"%ldkm", (NSInteger)self.travel.mileage];
    //喜欢
    self.likeCount.text = [NSString stringWithFormat:@"%@", self.travel.recommendations];
    self.attractionImage.image = [UIImage imageNamed:@"image_binder_poi_11"];
    //景点
    self.attraction.text = [NSString stringWithFormat:@"%@景点", self.travel.poi.sights];
    self.caterImage.image = [UIImage imageNamed:@"image_binder_poi_5"];
    //餐饮
    self.cater.text = [NSString stringWithFormat:@"%@餐饮",self.travel.poi.restaurant];
    [self.placeImage sd_setImageWithURL:[NSURL URLWithString:self.travel.icon] placeholderImage:[UIImage imageNamed:@""]];
    //地点
    NSString *string = nil;
    for (NSString *temp in self.travel.cities) {
        if (string == nil) {
            string = temp;
            continue;
        }
        string = [NSString stringWithFormat:@"%@•%@", string, temp];
    }
    self.place.text = string;
}

- (UIImage *)getImage {
    
    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.travel.cover_image];
    
    return cachedImage;
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    CJSCitiesViewController *citiesVC = [[CJSCitiesViewController alloc] init];
    citiesVC.spot_id = self.travel.Id;
    [self.delegate pushCitiesViewController:citiesVC];
}


@end
